<?php
class Admin_Model_DbTable_city extends Zend_Db_Table_Abstract
{
	 protected $_name = 'hae_bdd_city';
	 protected $_sequence=true;
     protected $_primary='id';
     protected $_referenceMap=array(
	'country'=>array(
		'columns'=>array('countryId'),
		'refTableClass'=>'Admin_Model_DbTable_country',
		'refColumns'=>array('id'),
		'onDelete'=>self::RESTRICT,
		'onUpdate'=>self::RESTRICT
	),
	'region'=>array(
		'columns'=>array('regionId'),
		'refTableClass'=>"Admin_Model_DbTable_region",
		'refColumns'=>array('id'),
		'onDelete'=>self::RESTRICT,
		'onUpdate'=>self::RESTRICT
	)
	);
	public function addCity($name_vi, $name_en, $name_ja, $name_cn, $name_fr, $countryId, $regionId, $loca_x, $loca_y)
	{
		$data = array(
		'name_vi' => $name_vi,
		'name_en' => $name_en,
		'name_ja' => $name_ja,
		'name_cn' => $name_cn,
		'name_fr' => $name_fr,
		'countryId' => $countryId,
		'regionId' => $regionId,
		'loca_x' => $loca_x,
		'loca_y' => $loca_y,
		);
		$this->insert($data);
		
	}
	public function getAllCity()
	{
		try {
			$select = $this->select();
			$select->from($this->_name,array("id","name_en","regionId"));
			return $this->fetchAll($select);
		}catch(Zend_Exception $e)
		{
			echo $e->getMessage();
		}
	}
	public function getByRegion($id)
	{
		try {
			$select = $this->select();
			$select->from($this->_name,array("id","name_en"));
			$select->where("regionId = ?",$id);
			return $this->fetchAll($select);
		}catch(Zend_Exception $e)
		{
			echo $e->getMessage();
		}
	}
	public function getByCountry($id)
	{
		try {
			$select = $this->select();
			$select->from($this->_name, array("id","name_en"));
			$select->where("countryId = ?",$id);
			return $this->fetchAll($select);
		}catch (Zend_Exception $e)
		{
			echo $e->getMessage();
		}
	}
	public function getCity($id)
	{
	    $id = (int)$id;
	    $row = $this->fetchRow('id=' . $id);
		if(!$row)
		{ 
			throw  new Exception("Could not find row {$id}");
		}
		return $row->toArray();
	}
	public function updateCity($id, $name_vi, $name_en, $name_ja, $name_cn, $name_fr, $countryId, $regionId, $loca_x, $loca_y)
	{
		$data = array(
		'name_vi' => $name_vi,
		'name_en' => $name_en,
		'name_ja' => $name_ja,
		'name_cn' => $name_cn,
		'name_fr' => $name_fr,
		'countryId' => $countryId,
		'regionId' => $regionId,
		'loca_x' => $loca_x,
		'loca_y' => $loca_y,
		);
		$this->update($data, 'id = '. (int)$id);
	}
	public function deleteCity($id)
    {
        return $this->delete('id =' . (int)$id);
    }
    public function findcity($name, $countryId, $regionId)
    {
    	
    	$select=$this->select();
    	
    	if ($name!=NULL)
    	{
			$select->where("name_en like '".$name."' OR name_vi like  '".$name."' OR name_ja like '".$name."' OR name_cn like '".$name."' OR name_fr like'".$name."'");
    	}
    	if ($countryId!=0)
    	{
			$select->where("countryId = ?",$countryId);
    	}
    	
    	if ($regionId!=0)
    	{
			$select->where("regionId = ?",$regionId);
    	}
    	
    	
    	return $this->fetchAll($select);
    }
	public function changestatus($id,$status)
	{
		$data = array(
		'status' => $status,
		);
		$this->update($data, 'id = '. (int)$id);
	}
}
?>